﻿using System.Collections.ObjectModel;
using System.Linq;
using Notes.Model;
using System.Windows;
using System;
using System.ComponentModel;
using System.Collections.Specialized;

namespace Notes.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Category> _Categories;
        public ObservableCollection<Category> Categories
        {
            get
            {
                return _Categories;
            }
            private set
            {
                _Categories = value;
                NotifyPropertyChanged("Categories");
            }
        }

        private ObservableCollection<Note> _Notes;
        public ObservableCollection<Note> Notes
        {
            get
            {
                return _Notes;
            }
            private set
            {
                _Notes = value;
                NotifyPropertyChanged("Notes");
            }
        }

        private ObservableCollection<Photo> _Photos;
        public ObservableCollection<Photo> Photos
        {
            get
            {
                return _Photos;
            }
            private set
            {
                _Photos = value;
                NotifyPropertyChanged("Photos");
            }
        }

        private bool _IsDataLoaded = false;
        public bool IsDataLoaded 
        { 
            get
            {
                return _IsDataLoaded;
            }
            private set
            {
                _IsDataLoaded = value;
                NotifyPropertyChanged("IsDataLoaded");
            }
        }

        public MainViewModel()
        {
            this.Notes = new ObservableCollection<Note>();
            this.Categories = new ObservableCollection<Category>();
            this.Photos = new ObservableCollection<Photo>();
        }

        public void LoadData()
        {
            var CategoriesInDB = from Category category in App.db.Categories where category.Deleted != true select category;
            this.Categories = new ObservableCollection<Category>(CategoriesInDB);

            var NotesInDB = from Note note in App.db.Notes where note.Deleted != true orderby note.Edited descending select note;
            this.Notes = new ObservableCollection<Note>(NotesInDB);

            var PhotosInDB = from Photo photo in App.db.Photos where photo.Deleted != true select photo;
            this.Photos = new ObservableCollection<Photo>(PhotosInDB);

            this.IsDataLoaded = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}